package com.pj.wisdomlogistics.ui.adapter;


import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.pj.wisdomlogistics.R;
import com.pj.wisdomlogistics.cons.Cons;
import com.pj.wisdomlogistics.entity.HomePageInfo;
import com.pj.wisdomlogistics.ui.view.recyclerview.BaseAdapter;
import com.pj.wisdomlogistics.ui.view.recyclerview.BaseViewHolder;
import com.pj.wisdomlogistics.utils.ScreenUtils;
import com.zhy.autolayout.utils.AutoUtils;

public class HomePageAdapter extends BaseAdapter<HomePageInfo.ListBean, BaseViewHolder> {
    private static int screenHeight;
    private static int screenWidth;
    public PopupWindow mPopWindow;
    private Activity mActivity;
    private View contentView;


    public HomePageAdapter(Context context, Activity mActivity) {
        super(context);
        this.mActivity = mActivity;

        screenWidth = ScreenUtils.getScreenWidth(context);
        screenHeight = ScreenUtils.getScreenHeight(context);

        contentView = LayoutInflater.from(context).inflate(R.layout.pop_delete, null);
        mPopWindow = new PopupWindow(contentView,
                (int) (screenWidth * 0.95), RelativeLayout.LayoutParams.WRAP_CONTENT, true);
        mPopWindow.setContentView(contentView);
        mPopWindow.setTouchable(true);
        mPopWindow.setBackgroundDrawable(ContextCompat.getDrawable(context , R.drawable.qipao));
        mPopWindow.setAnimationStyle(R.style.AnimationPreview);
        backgroundAlpha(1f);
        TextView tv = (TextView) contentView.findViewById(R.id.tv_delete);
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClick.editClick();
            }
        });
        mPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });
    }

    @Override
    public BaseViewHolder createCustomViewHolder(ViewGroup parent, int viewType) {
        return new BaseViewHolder(parent, R.layout.item_rv_main);
    }

    @Override
    public void bindCustomViewHolder(final BaseViewHolder viewHolder, int position) {
        AutoUtils.autoSize(viewHolder.itemView);

        final HomePageInfo.ListBean item = getItem(position);

        ImageView mIvType = viewHolder.getImageView(R.id.iv_type);
        TextView mTvState = viewHolder.getTextView(R.id.tv_state);
        TextView param1 = viewHolder.getTextView(R.id.tv_content);
        TextView param2 = viewHolder.getTextView(R.id.tv_date);

        viewHolder.getTextView(R.id.tv_type).setText(item.getTitle());


        switch (Integer.parseInt(item.getTag())){
            case 1:
                viewHolder.getImageView(R.id.iv_image).setImageResource(
                        item.getOrderType() == 0 ? R.drawable.parcel_application : R.drawable.order_application);
                mIvType.setBackgroundResource(R.drawable.parcel_s);
                mTvState.setText(Cons.takeoutList.get(item.getState()));
                param1.setText("订单详情：" + item.getParam2());
                param2.setText("下单时间：" + item.getParam1());
                break;
            case 2:
                viewHolder.getImageView(R.id.iv_image).setImageResource(R.drawable.car_application);
                mIvType.setBackgroundResource(R.drawable.car_s);
                mTvState.setText(Cons.carWaitList.get(item.getState()));
                param1.setText("申请时间：" + item.getParam1());
                param2.setText("用车时间：" + item.getParam2());
                break;
            case 3:
                viewHolder.getImageView(R.id.iv_image).setImageResource(R.drawable.meeting_application);
                mIvType.setBackgroundResource(R.drawable.meeting_s);
                mTvState.setText(Cons.statusWaitList.get(item.getState()));
                param1.setText("申请时间：" + item.getParam1());
                param2.setText("会议时间：" + item.getParam2());
                break;
            case 4:
                viewHolder.getImageView(R.id.iv_image).setImageResource(R.drawable.maintain_application);
                mIvType.setBackgroundResource(R.drawable.maintenance_s);
                mTvState.setText(Cons.repairList.get(item.getState()));
                param1.setText("报修事项：" + item.getParam2());
                param2.setText("申请时间：" + item.getParam1());
                break;
            case 5:
                viewHolder.getImageView(R.id.iv_image).setImageResource(R.drawable.canteen_application);
                mIvType.setBackgroundResource(R.drawable.canteen_s);
                mTvState.setText(Cons.canteenWaitList.get(item.getState()));
                param1.setText("申请时间：" + item.getParam1());
                param2.setText("用餐时间：" + item.getParam2());
                break;
        }

        viewHolder.getImageView(R.id.iv_edit).setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void onClick(View v) {
                showPopWindow(viewHolder.getImageView(R.id.iv_edit));
            }
        });

        viewHolder.getView(R.id.layout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClick.itemClick(item.getId() , Integer.parseInt(item.getTag()));
            }
        });
    }

    @Override
    public int getCustomViewType(int position) {
        return 0;
    }


    public onItemClick mOnItemClick;

    public interface onItemClick {
        public void editClick();
        public void itemClick(int id , int tag);
    }

    public void setListener(onItemClick mListener) {
        this.mOnItemClick = mListener;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void showPopWindow(View mView) {
        backgroundAlpha(0.3f);

        int[] mLocation = new int[2];
        mView.getLocationInWindow(mLocation);
        mPopWindow.showAtLocation(mView, Gravity.NO_GRAVITY, (int) (screenWidth * 0.05 / 2), mLocation[1] + mView.getHeight());

    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
        lp.alpha = bgAlpha; //0.0-1.0
        mActivity.getWindow().setAttributes(lp);
    }


}
